import http from '@/utils/http'
import { mockOrders, mockDelay, mockApiResponse } from '@/api/goods/mock'

// 是否使用模拟数据（开发时使用）
const USE_MOCK_DATA = false

/**
 * 获取订单列表 - 真实API调用
 * @param {Object} params - 查询参数
 */
export async function getOrderList(params) {
  if(USE_MOCK_DATA){
    // 降级到模拟数据
    const { status = 'all', page = 1, pageSize = 10 } = params
    
    // 根据状态过滤订单
    let filteredOrders = mockOrders
    if (status !== 'all') {
      const statusMap = {
        'pending': 'pending',
        'shipped': 'shipped', 
        'completed': 'completed',
        'cancelled': 'cancelled'
      }
      filteredOrders = mockOrders.filter(order => order.status === statusMap[status])
    }
    
    // 模拟分页
    const total = filteredOrders.length
    const startIndex = (page - 1) * pageSize
    const endIndex = startIndex + pageSize
    const list = filteredOrders.slice(startIndex, endIndex)
    
    return mockApiResponse({
      list,
      total,
      page: Number(page),
      pageSize: Number(pageSize),
      totalPages: Math.ceil(total / pageSize),
      hasMore: page * pageSize < total
    })
  }
  
  try {
    // 调用真实API
    const response = await http.get('/orders/list', params)
    return response
  } catch (error) {
    console.error('订单列表API调用失败:', error)
    throw error
  }
}

/**
 * 获取订单详情 - 调用真实API
 * @param {string} orderId - 订单ID
 */
export function getOrderDetail(orderId) {
  try {
    return http.get(`/orders/detail/${orderId}`)
  } catch (error) {
    console.error('获取订单详情失败:', error)
    throw error
  }
}

/**
 * 创建订单
 * @param {Object} orderData - 订单数据
 */
export async function createOrder(orderData) {
  try {
    const result = await http.post('/orders/create', orderData)
    return result
  } catch (error) {
    console.error('创建订单失败:', error)
    throw error
  }
}

/**
 * 取消订单
 * @param {string} orderId - 订单ID
 * @param {string} reason - 取消原因
 */
export async function cancelOrder(orderId, reason = '') {
  try {
    const result = await http.post(`/orders/${orderId}/cancel`, { reason })
    return result
  } catch (error) {
    console.error('取消订单失败:', error)
    throw error
  }
}

/**
 * 确认收货
 * @param {string} orderId - 订单ID
 */
export async function confirmOrder(orderId) {
  try {
    const result = await http.post(`/orders/${orderId}/confirm`)
    return result
  } catch (error) {
    console.error('确认收货失败:', error)
    throw error
  }
}

/**
 * 申请退款
 * @param {string} orderId - 订单ID
 * @param {Object} refundData - 退款数据
 */
export async function applyRefund(orderId, refundData) {
  try {
    const result = await http.post(`/orders/${orderId}/refund`, refundData)
    return result
  } catch (error) {
    console.error('申请退款失败:', error)
    throw error
  }
}

/**
 * 支付订单 - 真实API调用
 * @param {string} orderId - 订单ID
 * @param {Object} paymentData - 支付信息
 */
export function payOrder(orderId, paymentData) {
  try {
    // 调用真实API
    return http.post(`/orders/pay/${orderId}`, paymentData)
  } catch (error) {
    console.error('支付订单失败:', error)
    throw error
  }
}

/**
 * 获取订单统计数据 - 真实API调用
 */
export function getOrderStats() {
  try {
    // 调用真实API
    return http.get('/orders/stats')
  } catch (error) {
    console.error('获取订单统计失败:', error)
    throw error
  }
}

/**
 * 获取订单物流信息
 * @param {string} orderId - 订单ID
 */
export async function getOrderLogistics(orderId) {
  try {
    const result = await http.get(`/orders/${orderId}/logistics`)
    return result
  } catch (error) {
    console.error('获取订单物流失败:', error)
    throw error
  }
}

/**
 * 评价订单
 * @param {string} orderId - 订单ID
 * @param {Object} evaluationData - 评价数据
 */
export async function evaluateOrder(orderId, evaluationData) {
  try {
    const result = await http.post(`/orders/${orderId}/evaluate`, evaluationData)
    return result
  } catch (error) {
    console.error('评价订单失败:', error)
    throw error
  }
}

/**
 * 删除订单
 * @param {string} orderId - 订单ID
 */
export async function deleteOrder(orderId) {
  try {
    const result = await http.delete(`/orders/${orderId}`)
    return result
  } catch (error) {
    console.error('删除订单失败:', error)
    throw error
  }
}

/**
 * 重新购买
 * @param {string} orderId - 订单ID
 */
export async function rebuyOrder(orderId) {
  try {
    const result = await http.post(`/orders/${orderId}/rebuy`)
    return result
  } catch (error) {
    console.error('重新购买失败:', error)
    throw error
  }
}

/**
 * 获取订单发票信息
 * @param {string} orderId - 订单ID
 */
export async function getOrderInvoice(orderId) {
  try {
    const result = await http.get(`/orders/${orderId}/invoice`)
    return result
  } catch (error) {
    console.error('获取订单发票失败:', error)
    throw error
  }
}

/**
 * 申请发票
 * @param {string} orderId - 订单ID
 * @param {Object} invoiceData - 发票数据
 */
export async function applyInvoice(orderId, invoiceData) {
  try {
    const result = await http.post(`/orders/${orderId}/invoice`, invoiceData)
    return result
  } catch (error) {
    console.error('申请发票失败:', error)
    throw error
  }
} 